#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int t;
int W, H;
int x1, x2;
int y_1, y_2;
int w, h;

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> t;
    while (t--)
    {
        cin >> W >> H;
        cin >> x1 >> y_1 >> x2 >> y_2;
        cin >> w >> h;

        int res = 0x3f3f3f3f;
        if (x2 - x1 + w <= W)
        {
            //res = min(res, min(x2 - (W - w), w - x1));
            res = min(res, max(0, x2-(W-w)));
            res = min(res, max(0, w-x1));
        }

        if (y_2 - y_1 + h <= H)
        {
            // res = min(res, min(y_2 - (H - h), h - y_1));
            res = min(res, max(0, y_2-(H-h)));
            res = min(res, max(0, h-y_1));
        }

        if (res == 0x3f3f3f3f) res = -1;

        cout << res << endl;
    }
    return 0;
}
